package com.gw.business.system.role.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gw.business.common.DwzAjaxRes;
import com.gw.business.common.constant.GlobalConstant;
import com.gw.business.system.menu.model.TreeModel;
import com.gw.business.system.menu.service.ISystemService;
import com.gw.business.system.role.model.RoleModel;
import com.gw.business.system.role.model.RolePermissionsModel;
import com.gw.platform.controller.DefaultPageController;
import com.gw.platform.model.UserModel;
import com.gw.platform.page.impl.Page;
import com.gw.platform.util.StringUtils;

/**
 * 
 * 类功能:角色请求控制器
 * 
 *  
 * <p>创建者:关博</p>
 * 
 * <p>创建时间:2014-07-15</p>
 * 
 * <p>修改者:</p>
 * 
 * <p>修改时间:</p>
 * 
 * <p>修改原因：</p>
 * 
 * <p>审核者:</p>
 * 
 * <p>审核时间:</p>
 * 
 * <p>审核意见：</p>
 * 
 */
@Controller
@RequestMapping("role")
public class RoleController extends DefaultPageController{
	
	@Resource
	private ISystemService iSystemService;
	
	/**
	 * 分页查询所有信息
	 * 
	 * @return
	 */
	@RequestMapping("/page")
	public Callable<String> page() {
		return new Callable<String>() {
			public String call() throws Exception {
				String pageSize = request.getParameter("numPerPage");
				String pageNum = request.getParameter("pageNum");

				if(StringUtils.isEmpty(pageNum)){
					pageNum = "1";
					pageSize = Page.DEFAULT_PAGE_SIZE + "";;
				}
				// 设置当前页
				setCurrent(Long.parseLong(pageNum));
				
				page = defaultService.page("role.role_page", "role.role_total",
						getCurrent(), new HashMap<String, Object>(),
						Integer.parseInt(pageSize));
				// 设置分页操作的url
				request.setAttribute("page", page);
				return "system/role/page";
			}
		};
	}
	
	/**
	 * 跳转到添加页面
	 * @return
	 */
	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public String create() {
		return "system/role/create";
	}
	
	/**
	 * 跳转到加载角色页面
	 * @return
	 */
	@RequestMapping(value = "/update", method = RequestMethod.GET)
	public Callable<String> update() {
		return new Callable<String>() {
			public String call() throws Exception {
				
				Map map= new HashMap();
				map.put("id",request.getParameter("roleId"));
				
				RoleModel roleModel = (RoleModel) defaultService.load("role.role_info",map);
				
				request.setAttribute("roleModel", roleModel);
				
				return "system/role/update";
			}
		};
	}
	
	/**
	 * 更新系统角色
	 * @return
	 */
	@RequestMapping("/updateSave")
	@ResponseBody
	public Callable<Object> updateSave(final RoleModel role) {
		return new Callable<Object>() {
			public Object call() throws Exception {
				String callbackType = request.getParameter("callbackType");
				String navTabId = request.getParameter("navTabId");
				
				DwzAjaxRes res = new DwzAjaxRes();
				res.setCallbackType(callbackType);
				res.setNavTabId(navTabId);
				
				Map map= (Map) defaultService.load("role.role_loadByName",role);
				if(map != null){
					res.setStatusCode(DwzAjaxRes.STATUSCODE_300);
					res.setMessage(role.getRoleName()+ "角色已经存在！");
				}else{
					defaultService.update("role.role_update", role);
				}
				return res;
			}
		};
	}
	
	/**
	 * 删除
	 * @return
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public Callable<Object> delete(final UserModel user) {
		return new Callable<Object>() {
			public Object call() throws Exception {
				String callbackType = request.getParameter("callbackType");
				String navTabId = request.getParameter("navTabId");
				
				DwzAjaxRes res = new DwzAjaxRes();
				res.setCallbackType(callbackType);
				res.setNavTabId(navTabId);
				defaultService.load("role.role_delete",Integer.parseInt(request.getParameter("roleId")));
				
				return res;
			}
		};
	}
	
	/**
	 * 添加系统角色
	 * @return
	 */
	@RequestMapping("/insert")
	@ResponseBody
	public Callable<Object> insert(final RoleModel role) {
		return new Callable<Object>() {
			public Object call() throws Exception {
				String callbackType = request.getParameter("callbackType");
				String navTabId = request.getParameter("navTabId");
				
				DwzAjaxRes res = new DwzAjaxRes();
				res.setCallbackType(callbackType);
				res.setNavTabId(navTabId);
				
				Map map= (Map) defaultService.load("role.role_loadByName",role);
				if(map != null){
					res.setStatusCode(DwzAjaxRes.STATUSCODE_300);
					res.setMessage(role.getRoleName()+ "角色已经存在！");
				}else{
					role.setUserId(GlobalConstant.getSessionUser().getId());
					defaultService.insert("role.role_insert", role);
				}
				return res;
			}
		};
	}
	
	/**
	 * 获取角色对应的菜单权限
	 * @return
	 */
	@RequestMapping("/setPermissions")
	public Callable<String> setPermissions() {
		return new Callable<String>() {
			public String call() throws Exception {
				String roleId = request.getParameter("roleId");
				Map map = new HashMap();
				map.put("roleId", roleId);
				
				List<RolePermissionsModel> rolePermissionsList = (List<RolePermissionsModel>) defaultService
						.find("role.auths_Role_select", map);
				
				List list = iSystemService.getMeunTree("SystemMenu.System_loadAllMenu",null);
				
				for (int j=0;j<list.size();j++) {
		        	TreeModel current=(TreeModel) list.get(j);
		        	if(current.getItem() == null)	continue;
		        	  for (TreeModel item : current.getItem()) {
		        		  item.setSelect(false);
		        		  for (int i = 0; i < rolePermissionsList.size(); i++) {
								RolePermissionsModel model = rolePermissionsList.get(i);
								if(model.getMenuId().equals(item.getId())){
									item.setSelect(true);
									break;
					        	}
							}
		        	  }
				}
				
				request.setAttribute("rolePermissionsSystemMenu", list);
				
				request.setAttribute("roleId", roleId);
				return "system/role/setPermissions";
			}
		};
	}
	
	
	/**
	 * 添加系统角色菜单
	 * @return
	 */
	@RequestMapping("/savePermissionsSystemMenu")
	@ResponseBody
	public Callable<Object> savePermissionsSystemMenu(final RoleModel role) {
		return new Callable<Object>() {
			public Object call() throws Exception {
			    String names[] = request.getParameterValues("name");
			    DwzAjaxRes res = new DwzAjaxRes();
			    String callbackType = request.getParameter("callbackType");
				String navTabId = request.getParameter("navTabId");
				
				res.setCallbackType(callbackType);
				res.setNavTabId(navTabId);
				
		    	String roleId = request.getParameter("roleId");
				Map map = new HashMap();
				map.put("roleId", roleId);
				defaultService.delete("role.auths_Role_delete", map);
				
				if(names !=null){
					List<RolePermissionsModel> rpms = new ArrayList<RolePermissionsModel>();
					map.put("roleId", roleId);
					for (String m : names) {
						RolePermissionsModel rpm = new RolePermissionsModel();
						rpm.setMenuId(Integer.parseInt(m));
						rpm.setRoleId(Integer.parseInt(roleId));
						rpms.add(rpm);
					}
					defaultService.insert("role.auths_Role_insert", rpms);
			    }
				
				return res;
			}
		};
	}
}

